// components/userLogin/userLogin.js
let db = wx.cloud.database()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 注册
    userLogin(e){
      console.log(e)
      wx.showLoading({
        title: '请稍等...',
      })
      let that = this
      let date = new Date()
      if(e.detail.userInfo){
        e.detail.userInfo.date = date.getFullYear()+'-' +date.getMonth()+'-'+date.getDate()
        e.detail.userInfo.zhuli = 5
        e.detail.userInfo.count = 15
        db.collection('user').add({
          // data 字段表示需新增的 JSON 数据
          data: {
            userInfo:e.detail.userInfo,
            openId:wx.getStorageSync('openId')
          }
        })
        .then(res => {
          console.log(res)
          wx.setStorageSync('userInfo', e.detail.userInfo)
          wx.setStorageSync('loginTime', e.detail.userInfo.date)
          that.triggerEvent('isUserLogin')
        })
        .catch((e)=>{
          console.log(e)
          wx.hideLoading({
            success: (res) => {},
          })
        })
      } else {
        wx.hideLoading({
          success: (res) => {},
        })
      }
    }
  }
})
